//
//  MYGCredentialCell.swift
//  MYDoctor
//
//  Created by chenjs on 2024/4/27.
//

import UIKit
import QMUIKit
import TZImagePickerController

class MYGCredentialCell: UITableViewCell {
    private var currentButton :UIButton?
    
    var model:MYGCredentialCellModel? {
        didSet{
            
            guard let item = model else {
                return
            }
            
            tipLabel.text = item.title
            tipImageView.image = item.norImage
            Label.text = item.content
        }
        
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        
        contentView.addSubview(tipLabel)
        contentView.addSubview(tipImageView)
        contentView.addSubview(Label)
        contentView.addSubview(credentialButton1)
        contentView.addSubview(credentialButton2)
        contentView.addSubview(credentialButton3)
        
        tipLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().inset(25)
            make.height.equalTo(20)
        }
        
        tipImageView.snp.makeConstraints { make in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.height.equalTo(70)
            make.width.equalTo(140)
        }
        
        Label.snp.makeConstraints { make in
            make.top.equalTo(tipImageView)
            make.left.equalTo(tipImageView.snp.right).offset(10)
            make.right.equalToSuperview().inset(25)
        }
        
        credentialButton1.snp.makeConstraints { make in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipImageView.snp.bottom).offset(10)
            make.height.equalTo(110)
            make.width.equalTo((screenWidth - 70)/3)
        }
        
        credentialButton2.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(credentialButton1)
            make.height.equalTo(110)
            make.width.equalTo((screenWidth - 70)/3)
        }
        credentialButton3.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(25)
            make.top.equalTo(credentialButton1)
            make.height.equalTo(110)
            make.width.equalTo((screenWidth - 70)/3)
        }
        
    }
    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(51, 51, 51, 1)
        label.font = .regular(16)
        label.text = "身份证照片"
        return label
    }()
    
    private lazy var tipImageView:UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "")
        return v
    }()
    
    private lazy var Label: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(153, 153, 153, 1)
        label.font = .regular(12)
        label.text = "111"
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var credentialButton1: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Register/icon_tianjiatup"), for: .normal)
        btn.setTitle("执业证01", for: .normal)
        btn.setTitleColor(.rgba(81, 81, 81, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.spacingBetweenImageAndTitle = 10
        btn.imagePosition = .top
        btn.addTarget(self, action: #selector(selectImage(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var credentialButton2: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Register/icon_tianjiatup"), for: .normal)
        btn.setTitle("执业证02", for: .normal)
        btn.setTitleColor(.rgba(81, 81, 81, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.spacingBetweenImageAndTitle = 10
        btn.imagePosition = .top
        btn.addTarget(self, action: #selector(selectImage(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var credentialButton3: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Register/icon_tianjiatup"), for: .normal)
        btn.setTitle("执业证03", for: .normal)
        btn.setTitleColor(.rgba(81, 81, 81, 1), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.spacingBetweenImageAndTitle = 10
        btn.imagePosition = .top
        btn.addTarget(self, action: #selector(selectImage(_:)), for: .touchUpInside)
        return btn
    }()
    
}

extension MYGCredentialCell: TZImagePickerControllerDelegate {
    @objc
    private func selectImage(_ sender: QMUIButton) {
        self.currentButton = sender
        let pick = TZImagePickerController(maxImagesCount: 1, delegate: self)
        
        UIViewController.currentViewController?.present(pick!, animated: true)
    }
    
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        guard let image = photos.first else {
            return
        }
        if self.currentButton == credentialButton1 {
            model?.image1 = image
            credentialButton1.setImage(image, for: .normal)
        }else if self.currentButton == credentialButton2 {
            model?.image2 = image
            credentialButton2.setImage(image, for: .normal)
        }else{
            model?.image3 = image
            credentialButton3.setImage(image, for: .normal)
        }
        //model?.image = image
        //avatar.setImage(image, for: .normal)
    }
}
